﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scribe.Thrift;
using NCT.Framework;
using VISS.API.Commons;
using NCT.Scribe;

namespace VISS.API.Log {
    class LogBC : ILog {

        #region ILog Members

        public LogEntry RegisterEntry(string ip, string sessionID, string userKey) {
            return new LogEntry() {
                Message = string.Format("{0}\x01{1}\x01{2}\x01{3}\x01", ip, sessionID, userKey, DateTimeUtils.GetTimestamp()),
                Category = ConfigInfo.LOG_REGISTER
            };
        }

        void Register(string ip, string sessionID, string userName) {
            try {
                List<LogEntry> arrMsg = new List<LogEntry>();
                arrMsg.Add(RegisterEntry(ip, sessionID, userName));
                ResultCode result = ScribeClient.GetInstance(ConfigInfo.SCRIBE_INSTANCE).Log(arrMsg);
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("NCT.API.Commons.LogBC.Register({0}, {1}, {2}):\n", ip, sessionID, userName) + ex.ToString());
            }
        }

        private delegate void DelRegister(string ip, string sessionID, string userName);
        public void RegisterAsyn(string ip, string sessionID, string userKey) {
            DelRegister del = new DelRegister(Register);
            del.BeginInvoke(ip, sessionID, userKey, null, null);
        }

        public LogEntry LoginEntry(string ip, string sessionID, string userKey, bool isCookie) {
            return new LogEntry() {
                Message = string.Format("{0}\x01{1}\x01{2}\x01{3}\x01{4}\x01", ip, sessionID, userKey, isCookie, DateTimeUtils.GetTimestamp()),
                Category = ConfigInfo.LOG_LOGIN
            };
        }

        void Login(string ip, string sessionID, string userName, bool isCookie) {
            try {
                List<LogEntry> arrMsg = new List<LogEntry>();
                arrMsg.Add(LoginEntry(ip, sessionID, userName, isCookie));
                ResultCode result = ScribeClient.GetInstance(ConfigInfo.SCRIBE_INSTANCE).Log(arrMsg);
            }
            catch (System.Exception ex) {
                LogUtils.Error(string.Format("NCT.API.Commons.LogBC.Login({0}, {1}, {2}, {3}):\n", ip, sessionID, userName, isCookie) + ex.ToString());
            }
        }

        private delegate void DelLogin(string ip, string sessionID, string userName, bool isCookie);
        public void LoginAsyn(string ip, string sessionID, string userKey, bool isCookie) {
            DelLogin del = new DelLogin(Login);
            del.BeginInvoke(ip, sessionID, userKey, isCookie, null, null);
        }

        #endregion
    }
}
